package com.winchain.chainbill.bill.biz.receive.cmd;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICallback;
import com.shell.framework.command.ICommand;
import com.winchain.chainbill.bill.base.po.CbRcv;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.base.service.CbRcvService;
import com.winchain.chainbill.bill.base.service.CbXfeService;
import com.winchain.chainbill.bill.biz.Bill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillRcvAndXfeMoveCmd implements ICommand<Bill<CbRcv>>, ICallback<Bill<CbRcv>, AcctTxRec> {
	@Autowired
	private CbRcvService cbRcvService;
	@Autowired
	private CbXfeService cbXfeService;

	public boolean execute(CommandContext<Bill<CbRcv>> context) throws Exception {
		CbRcv cbRcv = (CbRcv) ((Bill) context.getSource()).getSource();

		String bizType = (String) context.getValue("bizType");
		if ("1".equals(cbRcv.getTxType())) {
			if (bizType.equals("cb_rcv")) {
				CbXfe cbXfe = new CbXfe();
				cbXfe.setBillId(cbRcv.getBillId());

				cbXfe.setXfeState("-1");
				cbXfe.setCurXfeState("1");
				cbRcv.setRcvState("-1");
				this.cbXfeService.updateCbXfeState(cbXfe);
			} else if (bizType.equals("cb_rc_aprv")) {
				CbRcv mcbRcv = new CbRcv();
				mcbRcv.setBillId(cbRcv.getBillId());

				mcbRcv.setRcvState("-2");
				mcbRcv.setCurRcvState("2");

				this.cbRcvService.updateDctState(mcbRcv);

				CbXfe cbXfe = new CbXfe();
				cbXfe.setBillId(cbRcv.getBillId());

				cbXfe.setXfeState("-2");
				cbXfe.setCurXfeState("2");
				this.cbXfeService.updateCbXfeState(cbXfe);

				cbXfe.setXfeState("2");
				cbXfe.setCurXfeState("1");
				this.cbXfeService.updateCbXfeState(cbXfe);
			}
		}
		return false;
	}

	public boolean callback(CallbackContext<Bill<CbRcv>, AcctTxRec> context) throws Exception {
		execute(context);
		return false;
	}
}
